part of '../index.dart';

abstract class FriendAPI {
  static List<String> getHistory() {
    final reslut = CacheUtil.get(Constants.kSearchHistory);
    if (reslut.isNotEmpty) {
      final list = (jsonDecode(reslut) as List)
          .map((element) => element.toString())
          .toList();
      return list;
    }
    return [];
  }

  static void saveHistory(String? keywords) {
    if (keywords == null || keywords.isEmpty) return;
    final list = getHistory();
    if (!list.contains(keywords)) {
      list.add(keywords);
      CacheUtil.set(Constants.kSearchHistory, jsonEncode(list));
    }
  }

  static void clearHistory() {
    CacheUtil.removeObject(Constants.kSearchHistory);
  }

  static Future<(List<UserSearchModel>, List<RoomSearchModel>)> search({
    showLoading = false,
    required int type, // 0-查询所有 1-查询用户 2-查询房间
    String? keywords,
    int pageNum = 1,
    int pageSize = 10,
  }) async {
    saveHistory(keywords);
    final resp = await HttpHelper.instance().post(
      '/api/v1/home-search',
      showLoading: showLoading,
      showToast: true,
      body: {
        'deviceNo': '00000000-0000-0000-0000-000000000000',
        'type': type,
        'keywords': keywords,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    List<UserSearchModel> users = [];
    List<RoomSearchModel> rooms = [];
    if (!resp.isSuccess || resp.result is! Map) {
      return (<UserSearchModel>[], <RoomSearchModel>[]);
    }
    if (resp.result['userList'] is List) {
      users = List.generate(
        resp.result['userList'].length,
        (index) => UserSearchModel.fromJson(resp.result['userList'][index]),
      );
    }
    if (resp.result['roomList'] is List) {
      for (var eleme in resp.result['roomList']) {
        if (eleme is! Map) continue;
        if (eleme['roomInfo'] is! Map) continue;
        rooms.add(RoomSearchModel.fromJson(eleme['roomInfo']));
      }
    }
    return (users, rooms);
  }

  static Future<List<RoomSearchModel>> recommendRooms({
    showLoading = false,
    int pageNum = 1,
    int pageSize = 10,
    required String country,
  }) async {
    final resp = await HttpHelper.instance().post(
      '/api/v3/pp-home/recommend-list?pageSize=$pageSize&pageNum=$pageNum&country=$country',
      showLoading: showLoading,
      showToast: true,
    );
    if (!resp.isSuccess || resp.result is! Map) return [];
    if (resp.result['recommend'] is! List) return [];
    return List.generate(
      resp.result['recommend'].length,
      (index) => RoomSearchModel.fromJson(resp.result['recommend'][index]),
    );
  }

  static Future<List<UserSearchModel>> recommendUsers(
      {showLoading = false}) async {
    final resp = await HttpHelper.instance().post(
      '/api/v3/topuser/ten',
      showLoading: showLoading,
      showToast: true,
    );
    if (!resp.isSuccess || resp.result is! List) return [];
    return List.generate(
      resp.result.length,
      (index) => UserSearchModel.fromJson(resp.result[index]),
    );
  }

  // 粉丝列表
  static Future<List<FriendModel>> fansList({
    showLoading = false,
    int pageNum = 1,
    int pageSize = 20,
    int type = 0, //0:全部 1:在线
  }) async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/user-friend/get-fans',
      showLoading: showLoading,
      showToast: true,
      body: {
        'type': type,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    if (!resp.isSuccess || resp.result is! Map) return [];
    if (resp.result['list'] is! List) return [];
    return List.generate(
      resp.result['list'].length,
      (index) => FriendModel.fromJson(resp.result['list'][index]),
    );
  }

  // 关注列表
  static Future<List<FriendModel>> followList({
    showLoading = false,
    int pageNum = 1,
    int pageSize = 20,
    int type = 0, //0:全部 1:在线
  }) async {
    final resp = await HttpHelper.instance().post(
      '/api/v1/user-friend/get-attentions',
      showLoading: showLoading,
      showToast: true,
      body: {
        'type': type,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    if (!resp.isSuccess || resp.result is! Map) return [];
    if (resp.result['list'] is! List) return [];
    return List.generate(
      resp.result['list'].length,
      (index) => FriendModel.fromJson(resp.result['list'][index]),
    );
  }

  // 好友列表
  static Future<List<FriendModel>> friendList({
    showLoading = false,
    int pageNum = 1,
    int pageSize = 20,
    int type = 0, //0:全部 1:在线
  }) async {
    final resp = await HttpHelper.instance().post(
      //'/api/v1/friend/friends',
      '/api/v1/user-friend/get-mutualAttentions',
      showLoading: showLoading,
      showToast: true,
      body: {
        'activeType': type,
        'pageNum': pageNum,
        'pageSize': pageSize,
      },
    );
    if (!resp.isSuccess || resp.result is! Map) return [];
    if (resp.result['list'] is! List) return [];
    return List.generate(
      resp.result['list'].length,
      (index) => FriendModel.fromJson(resp.result['list'][index]),
    );
  }
}
